package com.ptit.giaothong;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.view.KeyEvent;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class Setup extends Activity {

	private Activity activity;
	TextView textview;
	SeekBar seekbar;
	private AudioManager audioManager = null;

	public Setup(Activity activity) {
		this.activity = activity;
	}

	public void init() {
		MainActivity.keypage = 1;
		activity.setContentView(R.layout.activity_seekbar);
		

		textview = (TextView) activity.findViewById(R.id.textView);
		seekbar = (SeekBar) activity.findViewById(R.id.seekBar1);
		initControls();

		seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// add here your implementation
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// add here your implementation
			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
						progress, 0);
				textview.setText("Âm lượng " + progress);
			}
		});
	}

	private void initControls() {
		try {
			audioManager = (AudioManager) activity
					.getSystemService(Context.AUDIO_SERVICE);
			seekbar.setMax(audioManager
					.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
			seekbar.setProgress(audioManager
					.getStreamVolume(AudioManager.STREAM_MUSIC));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		switch (keyCode) {
		case KeyEvent.KEYCODE_VOLUME_UP:
			audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
					AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
			// Raise the Volume Bar on the Screen
			seekbar.setProgress(audioManager
					.getStreamVolume(AudioManager.STREAM_MUSIC));
			return true;
		case KeyEvent.KEYCODE_VOLUME_DOWN:
			// Adjust the Volume
			audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
					AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
			// Lower the VOlume Bar on the Screen
			seekbar.setProgress(audioManager
					.getStreamVolume(AudioManager.STREAM_MUSIC));
			return true;
		default:
			return false;
		}
	}

}
